package oris.audio;

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

/**
 *
 * @author u07459
 */
public class PlayerThread {

    static VoiceManager _voiceManager;
    static Voice _voice;
    static String _texto = new String();
    static boolean _pause = false;
    static boolean _play = false;
    static boolean _playando = false;

    public PlayerThread() {
        _voiceManager = VoiceManager.getInstance();
    }

    public void Fale(String texto) { 
        try{
            Stop();
        }catch(Exception ex){}

        _voice = _voiceManager.getVoice("kevin16");
        _voice.allocate();
        _voice.setVolume(0.75f);
        _texto = texto;
        _play = true;
        _voice.setPitch(100f);
        new Thread(new Falador()).start();
        _voice.setStyle("setStyle");
        //_voice.setPitch(500);
    }

    public void Pause() {
        _voice.getAudioPlayer().pause();
    }

    public void Resume() {
        _voice.getAudioPlayer().resume();
    }

    public void Stop() {
        _play = false;
        _playando = false;
        _voice.deallocate();
        _voice.getAudioPlayer().close();
    }

    public void SetVolume(float vol)
    {
        _voice.setVolume(vol);
    }
       
    public void SetPitch(float pitch)
    {
        _voice.setPitch(pitch);
        System.out.print(_voice.getPitch() + " - ");
        System.out.print(_voice.getPitchRange() + " - ");
        System.out.println(_voice.getPitchShift() + " - ");
    }
    
    public void SetTom(int tom)
    {
        
    }
    
    public void SetIdade(int idade)
    {
        
    }
    
    private static class Falador implements Runnable {

        public void run() {
            try {
                if (_play && !_playando) {
                    _playando = true;
                    _voice.speak(_texto);
                }
            } catch (Exception exception) {}
        }
    }
}
